package com.heartware.sh.blackhole.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.entity.test.MyEntity;

import java.util.List;

/**
 * Created by Administrator on 2016/11/28 0028.
 */
public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public final static int TYPE_FOOTER = 3;

    private List<MyEntity> list;

    public ListAdapter(List<MyEntity> list){
        this.list = list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == TYPE_FOOTER){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false);
            return new FooterViewHolder(view);
        }else{
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            return new MyViewHolder(view);
        }

//        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof  FooterViewHolder){

        }else{
            ((ListAdapter.MyViewHolder)holder).tv.setText(list.get(position).getTitle());
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    @Override
    public int getItemViewType(int position) {

        if(list.get(position) == null){
            return TYPE_FOOTER;
        }else{
            return 0;
        }

//        return super.getItemViewType(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        public TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }

    /**
     * Footer  加载更多按钮...
     */
    class FooterViewHolder extends RecyclerView.ViewHolder{

        public ProgressBar pb;

        public FooterViewHolder(View itemView) {
            super(itemView);
            pb = (ProgressBar) itemView.findViewById(R.id.pb);
        }

    }
}
